$(function() {
	$('#CourseTag').tagit({
		allowSpaces: true,
	});
	$.validator.addMethod('price', function(value, element) {
		return this.optional(element) || (parseInt(value) > 0);
	}, messages.price_must_be_large_than_0);
	$courseRules = {
		'data[Course][title]' : {
			required: true
		},
		'data[Course][description]': {
			required: true
		},
		'data[Course][target_user]': {
			required: true
		},
		'data[Course][price]': {
			required: true,
			number: true
		},
		'data[Course][course_type]': {
			required: true
		},
		'data[Course][course_image01]': {
			required: true,
			image: true
		},
		'data[Course][course_image02]': {
			image: true
		},
		'data[Course][course_image03]': {
			image: true
		}
	};
	if (isEdit) {
		$courseRules['data[Course][course_image01]'] = {
			image: true
		};
	}

	$courseErrorMessages = {
		'data[Course][title]' : {
			required: messages.course_title_is_required
		},
		'data[Course][description]': {
			required: messages.course_description_is_required
		},
		'data[Course][target_user]': {
			required: messages.course_target_is_required
		},
		'data[Course][price]': {
			required: messages.course_price_is_required,
			number: messages.price_must_be_numeric
		},
		'data[Course][course_type]': {
			required: messages.course_type_is_required
		},
		'data[Course][course_image01]': {
			required: messages.course_image_01_is_not_valid
		},// for courseDetail
		'data[CourseDetail][zip01]' : {
			required: messages.this_field_is_requied,
			number: messages.must_be_numeric
		},
		'data[CourseDetail][zip02]': {
			required: messages.this_field_is_requied,
			number: messages.must_be_numeric
		},
		'data[CourseDetail][address01]': {
			required: messages.this_field_is_requied
		},
		'data[CourseDetail][address02]': {
			required: messages.this_field_is_requied,
			number: messages.price_must_be_numeric
		},
		'data[CourseDetail][place_name]': {
			required: messages.this_field_is_requied
		},
		'data[CourseDetail][attend_total]': {
			required: messages.this_field_is_requied,
			number: messages.must_be_numeric
		},
		'data[CourseDetail][price]': {
			required: messages.this_field_is_requied,
			number: messages.must_be_numeric
		}
	};
	$.validator.addMethod('datetime', function(value, element) {
		return this.optional(element) || /^\d{4}\-\d{1,2}\-\d{1,2}\s\d{1,2}:\d{1,2}\:\d{1,2}$/i.test(value);
	}, messages.please_input_valid_datetime);
	$.validator.addMethod('image', function(value, element) {
		return this.optional(element) || /\.jpg|\.png|\.jpeg|\.gif$/i.test(value);
	}, messages.please_input_valid_image);
	$('#courseForm').validate({
		rules: $courseRules,
		messages: $courseErrorMessages,
		errorElement: 'div',
		errorClass: 'error-message',
		errorPlacement: function(error, element){
			if(element.attr('id') == "price"){
				error.insertAfter(".br-price");
			}else if(element.attr('id') == "zip01"){
				error.insertAfter("#zip02");
			}else{
				error.insertAfter(element);
			}
		}
	});
	
	$('#showScheduleForm').click(function(){
		$(this).hide();
		$('#isAddSchedule').val('1');
		$('#hiddenForm').show(500);
	});
	
	$('#hiddenScheduleForm').click(function(){
		$('#showScheduleForm').show();
		$('#isAddSchedule').val('0');
		$('#hiddenForm').hide(500);
	});
});